#!/bin/bash


:<<EOF
    封装这个功能作为一个函数,  来 加载 CSV 文件到 mysql中
EOF




# 构造函数
# @param  @notNull  csvpath              数据文件路径
# @param  @notNull  table                导入表
# @param  @notNull  columns              导入列
# @param  @notNull  import_date          导入日期
# @param  @notNull  env                  环境参数
# @param  @notNull  extend_part          sql 额外字段
function  LOAD_CSV_FUNCTION(){


    local  csvpath=$1
    local  table=$2
    local  columns=$3
    local  import_date=$4
    local  env=$5
    local  extend_part=$6


    LOAD_CSV_FUNCTION_RESP='' ;  #  return  result ;

    local script_path=$(dirname "$0")                           # 当前脚本路径
    BUILD_COMMAND_SQL=$script_path"/""BUILD_COMMAND_SQL.sh"
    source  "$BUILD_COMMAND_SQL"
    LOG4S=$script_path"/""LOG4S.sh"
    source  "$LOG4S"
    FILE_CONFIGURATION=$script_path"/""FILE_CONFIGURATION.sh"     #
    source  "$FILE_CONFIGURATION"


    BUILD_COMMAND_SQL  "'${csvpath}'"  ${table}   ${columns}    ${extend_part}

    local log_file_path="${LOG_FILE_FOLDER}/${env}/${import_date}/import_mer.log"
    if [ -w $log_file_path ]; then
        echo  "log_file_path ==>  ${log_file_path}"
    else
        mkdir  -p   $(dirname "$script_path")/var/dataPlatform/${env}/${import_date}
        echo  " creating  log_file_path ==>  ${log_file_path}"
        touch  ${log_file_path}
    fi


    if [[   ${env} = "prod" ]]; then
        config_file=$CONFIG_FILE_FOLDER/mysql_client_prod.conf
    elif [[  ${env} = "dev"   ]]; then
        config_file=$CONFIG_FILE_FOLDER/mysql_client_dev.conf
    else
        config_file=$CONFIG_FILE_FOLDER/mysql_client_local_dev.conf
    fi


    LOG4S_info  "执行sql ==>  ${FINAL_SQL}"       $log_file_path
    $(mysql --defaults-file="$config_file" -e "$FINAL_SQL")
    if [ $? -ne 0 ]; then
        LOAD_CSV_FUNCTION_RESP="写入 ${table} 失败"
        LOG4S_error  "$LOAD_CSV_FUNCTION_RESP ,  sql execute failed"         $log_file_path
    else
        LOAD_CSV_FUNCTION_RESP="写入 ${table} 成功"
        LOG4S_warn   "$LOAD_CSV_FUNCTION_RESP ,  sql execute succeed"        $log_file_path
    fi

}









